//
//  AuthenPhotoSampleAlertVM.swift
//  kxf
//
//  Created by LNS2 on 2024/4/5.
//

import Foundation
import Kingfisher

class AuthenPhotoSampleAlertVM: UIView {
    
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: SCREEN_HEIGHT))
        self.backgroundColor = WHColorWithAlpha(colorStr: "000000", alpha: 0)
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var closeImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "close_icon_gray")
        img.isUserInteractionEnabled = true
        img.isHidden = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(closeAction))
        img.addGestureRecognizer(tap)
        
        return img
    }()
    lazy var contentLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = WHColorWithAlpha(colorStr: "FFFFFF", alpha: 0.7)
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        lab.numberOfLines = 2
        lab.adjustsFontSizeToFitWidth = true
        lab.lineBreakMode = .byWordWrapping
        
        return lab
    }()
    lazy var imgView : UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        img.alpha = 0
        
        return img
    }()
}

extension AuthenPhotoSampleAlertVM{
    func showExampleImage(type:PHOTOTYPE) {
        self.isHidden = false
        switch type {
        case .mentou://小微门头
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.xiaoweiHeadSampleImgUrl, content: AuthenSampleImageModel.shareInstance.xiaoweiHeadSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.xiaoweiHeadSampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.xiaoweiHeadSampleDescrip
        case .diannei:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.xiaoweiShopOnUmgUrl, content: AuthenSampleImageModel.shareInstance.xiaoweiShopOnDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.xiaoweiShopOnUmgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.xiaoweiShopOnDescrip
        case .cash:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.xiaoweiCashSampleImgUrl, content: AuthenSampleImageModel.shareInstance.xiaoweiCashSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.xiaoweiCashSampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.xiaoweiCashSampleDescrip
        case .idCardFront:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.idCardFrontImgUrl, content: AuthenSampleImageModel.shareInstance.idCardFrontDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.idCardFrontImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.idCardFrontDescrip
        case .idCardBack:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.idCardBackSampleImgUrl, content: AuthenSampleImageModel.shareInstance.idCardBackSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.idCardBackSampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.idCardBackSampleDescrip
        case .idCardInHand:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.idCardInHandSampleImgUrl, content: AuthenSampleImageModel.shareInstance.idCardInHandSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.idCardInHandSampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.idCardInHandSampleDescrip
        case .bankFront:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.bankCardSampleImgUrl, content: AuthenSampleImageModel.shareInstance.bankCardSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.bankCardSampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.bankCardSampleDescrip
        case .kaihuxuke:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.bankCardXuKeSampleUmgUrl, content: AuthenSampleImageModel.shareInstance.bankCardXuKeSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.bankCardXuKeSampleUmgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.bankCardXuKeSampleDescrip
        case .cashCompany:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.companyCashSampleImgUrl, content: AuthenSampleImageModel.shareInstance.companyCashSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.companyCashSampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.companyCashSampleDescrip
        case .mentouCompany:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.companyHeadSampleImgUrl, content: AuthenSampleImageModel.shareInstance.companyHeadSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.companyHeadSampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.companyHeadSampleDescrip
        case .dianneiCompany:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.companySampleImgUrl, content: AuthenSampleImageModel.shareInstance.companySampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.companySampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.companySampleDescrip
        case .dianwaiCompany:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.companyOutSampleImgUrl, content: AuthenSampleImageModel.shareInstance.companyOutSampleDescrip)
//            imgView.setImgUrl(urlString: AuthenSampleImageModel.shareInstance.companyOutSampleImgUrl)
//            contentLabel.text = AuthenSampleImageModel.shareInstance.companyOutSampleDescrip
        case .licence:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.lincenceSampleImgUrl, content: AuthenSampleImageModel.shareInstance.lincenceSampleDescrip)
        case .location:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.shopLocationSampleImgUrl, content: AuthenSampleImageModel.shareInstance.shopLocationSampleDescrip)
        case .proofMchnt:
            self.setImgUrlString(imgUrlString: AuthenSampleImageModel.shareInstance.proofMchntSampleImgUrl, content: AuthenSampleImageModel.shareInstance.proofMchntSampleDescrip)
        default: break
            
        }
        
        UIView.animate(withDuration: 0.3, delay: 0,options: .curveLinear) {
            self.imgView.alpha = 1
        }completion: { t in
            self.backgroundColor =  WHColorWithAlpha(colorStr: "000000", alpha: 0.85)
            self.closeImgView.isHidden = false
        }
    }
    @objc func closeAction(){
        self.closeImgView.isHidden = true
        UIView.animate(withDuration: 0.3, delay: 0,options: .curveLinear) {
            self.imgView.alpha = 0
            self.backgroundColor =  WHColorWithAlpha(colorStr: "000000", alpha: 0)
        }completion: { t in
            self.isHidden = true
        }
    }
    func setImgUrlString(imgUrlString:String,content:String) {
        
        let imgUrl = URL(string: imgUrlString)
        imgView.kf.setImage(with: imgUrl,options: [.transition(.fade(0.2))], completionHandler:  { result in
            let imgOriSize = self.imgView.image?.size
            let imgWidth = SCREEN_WIDHT
            let imgHeight = imgWidth * ((imgOriSize?.height ?? 0) / (imgOriSize?.width ?? 1))
            
            let imgOriginY = (SCREEN_HEIGHT-imgHeight)*0.5
            
            let imgRect = CGRect.init(x: 0, y: imgOriginY, width: SCREEN_WIDHT, height: imgHeight)
            self.imgView.frame = imgRect
            
            if imgOriginY < WHBaseViewVC().getNavigationBarHeight(){
                self.contentLabel.snp.remakeConstraints { make in
                    make.left.equalTo(kFitWidth(12))
                    make.right.equalTo(kFitWidth(-12))
                    make.centerX.lessThanOrEqualToSuperview()
                    make.top.equalTo(WHBaseViewVC().getNavigationBarHeight())
                }
            }else{
                self.contentLabel.snp.remakeConstraints { make in
                    make.left.equalTo(kFitWidth(12))
                    make.right.equalTo(kFitWidth(-12))
                    make.centerX.lessThanOrEqualToSuperview()
                    make.bottom.equalTo(self.imgView.snp.top).offset(kFitWidth(-10))
                }
            }
            DLLog(message: "imgUrlString:\(imgUrlString)")
            DLLog(message: "content:\(content)")
            self.contentLabel.text = content
        })
    }
}
    
extension AuthenPhotoSampleAlertVM{
    func initUI() {
        addSubview(imgView)
        addSubview(contentLabel)
        addSubview(closeImgView)
        
        setConstrait()
    }
    func setConstrait() {
//        imgView.snp.makeConstraints { make in
//            make.centerY.lessThanOrEqualToSuperview()
//            make.width.equalToSuperview()
//        }
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(12))
            make.right.equalTo(kFitWidth(-40))
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(WHBaseViewVC().getNavigationBarHeight())
//            make.bottom.equalTo(imgView.snp.top).offset(kFitWidth(-10))
        }
        closeImgView.snp.makeConstraints { make in
            make.width.height.equalTo(kFitWidth(30))
            make.top.equalTo(statusBarHeight+kFitWidth(7))
            make.right.equalTo(kFitWidth(-12))
        }
    }
}
